*******************************************************************************************************************************
/*Replicate results for "Distinguishing barriers to insurance in Thai villages"*/
**.do File for generating the tables for submitted version to JHR
**Creator: Cynthia Kinnan and Ozgur Bozcaga
**To locate code that replicates the table, just search the do file using the exact table name OR the table number
*******************************************************************************************************************************

*** Set Paths and globals
set matsize 3200

clear
global root = "/Users//`=c(username)'/dropbox/Insurance/JHR/Tables/"
cd "$root"
global tex_tables = "$root/tex_tables/"


****************************************************************************************************
*TABLES IN THE MAIN TEXT
****************************************************************************************************
*-------------------------------------------------------------------------------
**** Table 1 : Consumption smoothing at the individual and village level
*-------------------------------------------------------------------------------


** Import Annual Data for Yearly Estimates
use annual.dta , clear

* (1) Model with Village-year FE:
xtset, clear
xtset vill_year

* (1.1) OLS (Table 1 - Panel A - Column 1):
areg dm_lnexp dm_lninc, absorb(vill_year)
estadd local Sample "All"
estadd local Fixed "Vill-yr"
estadd scalar fstat = e(F_absorb) 
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store ols_year

local dm_rainfall_q1 "dm_rain_dev1X7 dm_rain_dev1_2X7 dm_rain_dev1X9 dm_rain_dev1_2X9 dm_rain_dev1X11 dm_rain_dev1_2X11 dm_rain_dev1X13 dm_rain_dev1_2X13 dm_rain_dev1X15 dm_rain_dev1_2X15 dm_rain_dev1X19 dm_rain_dev1_2X19 dm_rain_dev1X23 dm_rain_dev1_2X23 dm_rain_dev1X31 dm_rain_dev1_2X31 dm_rain_dev1X61 dm_rain_dev1_2X61 dm_rain_dev1X65 dm_rain_dev1_2X65"
local dm_rainfall_q2 "dm_rain_dev2X7 dm_rain_dev2_2X7 dm_rain_dev2X9 dm_rain_dev2_2X9 dm_rain_dev2X11 dm_rain_dev2_2X11 dm_rain_dev2X13 dm_rain_dev2_2X13 dm_rain_dev2X15 dm_rain_dev2_2X15 dm_rain_dev2X19 dm_rain_dev2_2X19 dm_rain_dev2X23 dm_rain_dev2_2X23 dm_rain_dev2X31 dm_rain_dev2_2X31 dm_rain_dev2X61 dm_rain_dev2_2X61 dm_rain_dev2X65 dm_rain_dev2_2X65"
local dm_rainfall_q3 "dm_rain_dev3X7 dm_rain_dev3_2X7 dm_rain_dev3X9 dm_rain_dev3_2X9 dm_rain_dev3X11 dm_rain_dev3_2X11 dm_rain_dev3X13 dm_rain_dev3_2X13 dm_rain_dev3X15 dm_rain_dev3_2X15 dm_rain_dev3X19 dm_rain_dev3_2X19 dm_rain_dev3X23 dm_rain_dev3_2X23 dm_rain_dev3X31 dm_rain_dev3_2X31 dm_rain_dev3X61 dm_rain_dev3_2X61 dm_rain_dev3X65 dm_rain_dev3_2X65"
local dm_rainfall_q4 "dm_rain_dev4X7 dm_rain_dev4_2X7 dm_rain_dev4X9 dm_rain_dev4_2X9 dm_rain_dev4X11 dm_rain_dev4_2X11 dm_rain_dev4X13 dm_rain_dev4_2X13 dm_rain_dev4X15 dm_rain_dev4_2X15 dm_rain_dev4X19 dm_rain_dev4_2X19 dm_rain_dev4X23 dm_rain_dev4_2X23 dm_rain_dev4X31 dm_rain_dev4_2X31 dm_rain_dev4X61 dm_rain_dev4_2X61 dm_rain_dev4X65 dm_rain_dev4_2X65"


* (1.2) IV (Table 1- Panel A - Column 2):
xtivreg dm_lnexp (dm_lninc =`dm_rainfall_q1' `dm_rainfall_q2' `dm_rainfall_q3' `dm_rainfall_q4') , fe 
estadd local Sample "All"
estadd local Fixed "Vill-yr"
estadd scalar fstat = e(F_f) 
estadd scalar obs= e(N)
estimates store iv_year


** Import Monthly Data for Monthly Estimates
use monthly.dta ,clear
xtset, clear
xtset vill_mo

* (2) Model with Village-month FE:

* (2.1) OLS (Table 1- Panel A - Column 3):
areg dm_exp_ihs dm_inc_ihs, absorb(vill_mo)
estadd local Sample "All"
	estadd local Fixed "Vill-mnth"
	estadd scalar fstat = e(F_absorb) 
	estadd scalar obs= e(N)
	estadd scalar r= e(r2)
estimates store ols_month

local dm_rainfall "dm_rain_dev dm_sq_rain_dev dm_rain_devX7 dm_sq_rain_devX7 dm_rain_devX9 dm_sq_rain_devX9 dm_rain_devX11 dm_sq_rain_devX11 dm_rain_devX13 dm_sq_rain_devX13 dm_rain_devX15 dm_sq_rain_devX15 dm_rain_devX19 dm_sq_rain_devX19 dm_rain_devX23 dm_sq_rain_devX23 dm_rain_devX31 dm_sq_rain_devX31 dm_rain_devX61 dm_sq_rain_devX61 dm_rain_devX65 dm_sq_rain_devX65"
local dm_rainfall_l2 "dm_rain_dev_l2 dm_sq_rain_dev_l2 dm_rain_devX7_l2 dm_sq_rain_devX7_l2 dm_rain_devX9_l2 dm_sq_rain_devX9_l2 dm_rain_devX11_l2 dm_sq_rain_devX11_l2 dm_rain_devX13_l2 dm_sq_rain_devX13_l2 dm_rain_devX15_l2 dm_sq_rain_devX15_l2 dm_rain_devX19_l2 dm_sq_rain_devX19_l2 dm_rain_devX23_l2 dm_sq_rain_devX23_l2 dm_rain_devX31_l2 dm_sq_rain_devX31_l2 dm_rain_devX61_l2 dm_sq_rain_devX61_l2 dm_rain_devX65_l2 dm_sq_rain_devX65_l2"
local dm_rainfall_l3 "dm_rain_dev_l3 dm_sq_rain_dev_l3 dm_rain_devX7_l3 dm_sq_rain_devX7_l3 dm_rain_devX9_l3 dm_sq_rain_devX9_l3 dm_rain_devX11_l3 dm_sq_rain_devX11_l3 dm_rain_devX13_l3 dm_sq_rain_devX13_l3 dm_rain_devX15_l3 dm_sq_rain_devX15_l3 dm_rain_devX19_l3 dm_sq_rain_devX19_l3 dm_rain_devX23_l3 dm_sq_rain_devX23_l3 dm_rain_devX31_l3 dm_sq_rain_devX31_l3 dm_rain_devX61_l3 dm_sq_rain_devX61_l3 dm_rain_devX65_l3 dm_sq_rain_devX65_l3"
local dm_rainfall_l4 "dm_rain_dev_l4 dm_sq_rain_dev_l4 dm_rain_devX7_l4 dm_sq_rain_devX7_l4 dm_rain_devX9_l4 dm_sq_rain_devX9_l4 dm_rain_devX11_l4 dm_sq_rain_devX11_l4 dm_rain_devX13_l4 dm_sq_rain_devX13_l4 dm_rain_devX15_l4 dm_sq_rain_devX15_l4 dm_rain_devX19_l4 dm_sq_rain_devX19_l4 dm_rain_devX23_l4 dm_sq_rain_devX23_l4 dm_rain_devX31_l4 dm_sq_rain_devX31_l4 dm_rain_devX61_l4 dm_sq_rain_devX61_l4 dm_rain_devX65_l4 dm_sq_rain_devX65_l4"
local dm_rainfall_l5 "dm_rain_dev_l5 dm_sq_rain_dev_l5 dm_rain_devX7_l5 dm_sq_rain_devX7_l5 dm_rain_devX9_l5 dm_sq_rain_devX9_l5 dm_rain_devX11_l5 dm_sq_rain_devX11_l5 dm_rain_devX13_l5 dm_sq_rain_devX13_l5 dm_rain_devX15_l5 dm_sq_rain_devX15_l5 dm_rain_devX19_l5 dm_sq_rain_devX19_l5 dm_rain_devX23_l5 dm_sq_rain_devX23_l5 dm_rain_devX31_l5 dm_sq_rain_devX31_l5 dm_rain_devX61_l5 dm_sq_rain_devX61_l5 dm_rain_devX65_l5 dm_sq_rain_devX65_l5"

local rainfall_l2 "rain_dev_l2 sq_rain_dev_l2 rain_devX7_l2 sq_rain_devX7_l2 rain_devX9_l2 sq_rain_devX9_l2 rain_devX11_l2 sq_rain_devX11_l2 rain_devX13_l2 sq_rain_devX13_l2 rain_devX15_l2 sq_rain_devX15_l2 rain_devX19_l2 sq_rain_devX19_l2 rain_devX23_l2 sq_rain_devX23_l2 rain_devX31_l2 sq_rain_devX31_l2 rain_devX61_l2 sq_rain_devX61_l2 rain_devX65_l2 sq_rain_devX65_l2"
local rainfall_l3 "rain_dev_l3 sq_rain_dev_l3 rain_devX7_l3 sq_rain_devX7_l3 rain_devX9_l3 sq_rain_devX9_l3 rain_devX11_l3 sq_rain_devX11_l3 rain_devX13_l3 sq_rain_devX13_l3 rain_devX15_l3 sq_rain_devX15_l3 rain_devX19_l3 sq_rain_devX19_l3 rain_devX23_l3 sq_rain_devX23_l3 rain_devX31_l3 sq_rain_devX31_l3 rain_devX61_l3 sq_rain_devX61_l3 rain_devX65_l3 sq_rain_devX65_l3"
local rainfall_l4 "rain_dev_l4 sq_rain_dev_l4 rain_devX7_l4 sq_rain_devX7_l4 rain_devX9_l4 sq_rain_devX9_l4 rain_devX11_l4 sq_rain_devX11_l4 rain_devX13_l4 sq_rain_devX13_l4 rain_devX15_l4 sq_rain_devX15_l4 rain_devX19_l4 sq_rain_devX19_l4 rain_devX23_l4 sq_rain_devX23_l4 rain_devX31_l4 sq_rain_devX31_l4 rain_devX61_l4 sq_rain_devX61_l4 rain_devX65_l4 sq_rain_devX65_l4"
local rainfall_l5 "rain_dev_l5 sq_rain_dev_l5 rain_devX7_l5 sq_rain_devX7_l5 rain_devX9_l5 sq_rain_devX9_l5 rain_devX11_l5 sq_rain_devX11_l5 rain_devX13_l5 sq_rain_devX13_l5 rain_devX15_l5 sq_rain_devX15_l5 rain_devX19_l5 sq_rain_devX19_l5 rain_devX23_l5 sq_rain_devX23_l5 rain_devX31_l5 sq_rain_devX31_l5 rain_devX61_l5 sq_rain_devX61_l5 rain_devX65_l5 sq_rain_devX65_l5"

* (2.2) IV using six month rainfall (Table 1- Panel A - Column 4):
xtivreg dm_exp_ihs (dm_inc_ihs =`dm_rainfall' `dm_rainfall_l2' `dm_rainfall_l3' `dm_rainfall_l4' `dm_rainfall_l5'), fe 
estadd local Sample "All"
estadd local Fixed "Vill-yr"
estadd scalar fstat = e(F_f) 
estadd scalar obs= e(N)
estimates store iv_month


* (2.3) OLS (Table 1- Panel A - Column 5 and Column 6): 
areg dm_exp_ihs dm_inc_ihs if highR2 == 1, a(vill_mo)
estadd local Sample "High R2"
estadd local Fixed "Vill-mnth"
estadd scalar fstat = e(F_absorb) 
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store ols_month_highR2

areg dm_exp_ihs dm_inc_ihs if lowR2 == 1, a(vill_mo)
estadd local Sample "Low R2"
estadd local Fixed "Vill-mnth"
estadd scalar fstat = e(F_absorb) 
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store ols_month_lowR2

*** Export Panel A of Table 1
esttab ols_year iv_year ols_month iv_month ols_month_highR2 ols_month_lowR2 using "${tex_tables}T1PA.tex" , ///
replace noobs b(%10.4f) se wrap nostar  ///
scalars("Sample Sample" "Fixed Fixed Effects"  "fstat FE F statistic" "obs Observations" "r R-squared") sfmt(%10.3f %10.3f %10.3f %10.0f %10.3f) ///
align(cccc) label nogaps numbers nonotes keep(dm_lninc dm_inc_ihs) mtitles ("OLS" "IV" "OLS" "IV" "OLS" "OLS") ///
mgroups("\multicolumn{2}{c}{IHS household annual PCE} & \multicolumn{2}{c}{IHS household monthly PCE} \\ \cline{2-3} \cline{4-7}", pattern(1 0 1 0))
			
* (3) Results aggregated at the village level:

** Import Annual Village Level Data Set
use annual_vill.dta , clear
tsset,clear
tsset vill year

* (3.1) OLS Village Avg Annually (Table 1- Panel B - Column 1)
reg lnexp_v lninc_v, nocons vce(cluster vill_yr) 
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store ols_year_agg

** Import Monthly Village Level Data Set
use monthly_vill.dta ,clear
tsset,clear
tsset vill month

* (3.2) OLS Village Avg Monthly (Table 1- Panel B - Column 2)
reg lnexp_v inc_ihs_v, vce(cluster vill_qr)
estadd scalar obs= e(N)
estadd scalar r= e(r2) 
estimates store ols_month_agg 

*** Export Panel B of Table 1
esttab ols_year_agg ols_month_agg  using "${tex_tables}T1PB.tex" , ///
replace noobs b(%10.4f) se wrap nostar ///
scalars("obs Observations" "r R-squared") sfmt(%10.0f %10.3f) ///
align(cccc) label nogaps numbers nonotes keep(lninc_v inc_ihs_v) mtitles ("OLS" "OLS") 
			

*-------------------------------------------------------------------------------
**** Table 2 : Testing the hidden income model: Predictive role of lagged income
*-------------------------------------------------------------------------------
eststo clear 

*Import Data 
use lagged_aggregate_stay ,clear 

*we first define the proper clustering:
global clust vill_qr

* (1) models with rho=1
*Linear model (no splines) 
*with the interactions (model 1) (Table 2- Column 1): 
reg exp lag_Exp exp_eta eta l_inc_ihs if l_income>=0, vce(cluster $clust)
estadd local rho "1"
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model1

local fspline_exp "lag_expf1 lag_expf2 lag_expf3"

*run only quadratic splines for consumption (model 2) (Table 2- Column 2):
reg exp `fspline_exp' exp_eta eta l_inc_ihs if l_income>=0, nocons vce(cluster $clust)
estadd local rho "1"
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model2

gen it=eta*lag_Exp
reg exp lag_Exp eta it $incvar
gen sample=e(sample)

gen lag_exp_eta2=lag_Exp*eta

bspline, xvar(eta) power(2) generate(eta2_q)
bspline, xvar(lag_exp_eta2) power(2) generate(lag_exp_eta2_q)

flexcurv, xvar(eta) power(2) generate(eta2_f) refpts(1475.881 5347.684 21954.77)
flexcurv, xvar(lag_exp_eta2) power(2) generate(lag_exp_eta2_f) refpts(545449.9  3.21e+07 3.01e+09)


*run splines and interactions with eta (model 3) (Table 2- Column 3):
reg exp lag_expq1 lag_expq2 lag_expq3 eta2_q1 eta2_q2 eta2_q3 lag_exp_eta2_q1 lag_exp_eta2_q2 lag_exp_eta2_q3 l_inc_ihs if l_income>=0 & l_income!=. , nocons vce(cluster $clust)
estadd local rho "1"
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model3
gen sample4=e(sample)


*(2) models with rho!=1
*NLS with consumption, eta and interactions entering linearly (model 4) (Table 2- Column 4):
eststo : nl (exp= ({gamma}*(lag_Exp)^{rho=0.5} + {beta}*(eta)+ {alpha}*(eta)*(lag_Exp)^{rho=0.5} + {nu}*l_inc_ihs)^(1/{rho=0.5})) if l_income>=0 & l_income!=., vce(cluster $clust)
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "NLLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model4

*NLS with consumption, eta and interactions entering as splines (model 5) (Table 2- Column 5):
eststo: nl (exp= ({gamma1}*(lag_expq1)^{rho=0.5} + {gamma2}*(lag_expq2)^{rho=0.5} + {gamma3}*(lag_expq3)^{rho=0.5}  ///
+{beta1}*(eta2_q1) +{beta2}*(eta2_q2) + {beta3}*(eta2_q3) ///
+ {alpha1}*lag_exp_eta2_q1^{rho=0.5} + {alpha2}*lag_exp_eta2_q2^{rho=0.5} +  {alpha3}*lag_exp_eta2_q3^{rho=0.5} ///
+ {nu}* l_inc_ihs)^(1/{rho=0.5})) if sample4, vce(cluster $clust ) 
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "NLLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model5

*** Export Table 2
esttab model1 model2 model3 using "${tex_tables}T2.tex" , ///
replace  noobs b(%10.3f) se wrap nostar ///
scalars("rho Rho(CRRA curvature)" "Village Village-month fixed effect?" "Villagec Village-month fixed effect x ct−1?" "Estimate Estimation Method" "obs Observations" "r R-squared") sfmt(%10.0f %10.0f %10.0f %10.0f %10.0f %10.3f) ///
align(cccc) label nogaps numbers nomtitles nonotes keep(l_inc_ihs lag_Exp)

*-------------------------------------------------------------------------------
**** Table 3 : Testing the hidden income model: Heterogeneity by predictive power of rainfall
*-------------------------------------------------------------------------------
estimates clear

*Import Data 
use lagged_aggregate_stay ,clear 

*we first define the proper clustering:
global clust vill_qr

* (1) models with rho=1
*Linear model (no splines):
*with the interactions (Model 1) (Table 3 : Column 1):
reg exp lag_Exp exp_eta eta r2_rain l_inc_ihs r2_l_inc_ihs if l_income>=0 & r2_rain!=., vce(cluster $clust )
estadd local rho "1"
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model1

*Non-linear models:
local fspline_exp "lag_expf1 lag_expf2 lag_expf3"
local fspline_eta "etaf1 etaf2 etaf3"
local fspline_exp_eta "exp_etaf1 exp_etaf2 exp_etaf3"

*we first run only quadratic splines for consumption (model 2) (Table 3 : Column 2):
reg exp `fspline_exp' exp_eta eta r2_rain l_inc_ihs r2_l_inc_ihs if l_income>=0 & r2_rain!=., nocons vce(cluster $clust )
estadd local rho "1"
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model2

*we now run quadratic splines for all variables (model 3) (Table 3 : Column 3):
reg exp `fspline_exp' `fspline_eta' `fspline_exp_eta' r2_rain l_inc_ihs r2_l_inc_ihs if l_income>=0 & r2_rain!=., nocons vce(cluster $clust )
estadd local rho "1"
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model3

gen it=eta*lag_Exp
reg exp lag_Exp eta it $incvar

gen lag_exp_eta2=lag_Exp*eta

bspline, xvar(eta) power(2) generate(eta2_q)
bspline, xvar(lag_exp_eta2) power(2) generate(lag_exp_eta2_q)

flexcurv, xvar(eta) power(2) generate(eta2_f) refpts(1475.881 5347.684 21954.77)
flexcurv, xvar(lag_exp_eta2) power(2) generate(lag_exp_eta2_f) refpts(545449.9  3.21e+07 3.01e+09)


* (2) models with rho!=1
*NLS with consumption, eta and interactions entering linearly (model 4) (Table 3 : Column 4):
nl (exp= ({gamma}*(lag_Exp)^{rho=0.5} + {beta}*(eta)+ {alpha}*(eta)*(lag_Exp)^{rho=0.5} +  {nu0}*r2_rain+{nu1}*l_inc_ihs+{nu2}*r2_l_inc_ihs)^(1/{rho=0.5})) if l_income>=0 & l_income!=. & r2_rain!=., vce(cluster $clust )
estimates store model4

*NLS with consumption, eta and interactions entering as splines (model 5) (Table 3 : Column 5):
reg exp lag_expq1 lag_expq2 lag_expq3 eta2_q1 eta2_q2 eta2_q3 lag_exp_eta2_q1 lag_exp_eta2_q2 lag_exp_eta2_q3 $incvar r2_rain if l_income>=0 & l_income!=.
gen sample5=e(sample)

nl (exp= ({gamma1}*(lag_expq1)^{rho=0.5} + {gamma2}*(lag_expq2)^{rho=0.5} +  ///
{gamma3}*(lag_expq3)^{rho=0.5}  +{beta1}*(eta2_q1) +{beta2}*(eta2_q2) +  ///
{beta3}*(eta2_q3) + {alpha1}*lag_exp_eta2_q1^{rho=0.5} +  ///
{alpha2}*lag_exp_eta2_q2^{rho=0.5} +  {alpha3}*lag_exp_eta2_q3^{rho=0.5}+  ///
{nu0}*r2_rain+{nu1}*l_inc_ihs+{nu2}*r2_l_inc_ihs)^(1/{rho=0.5})) if sample5 & r2_rain!=., vce(cluster $clust )
estimates store model5


*** Export Table 3
esttab model1 model2 model3 using "${tex_tables}T3.tex" , ///
replace  noobs b(%10.2f) se wrap nostar ///
scalars("rho Rho(CRRA curvature)" "Village Village-month fixed effect?" "Villagec Village-month fixed effect  ct−1?" "Estimate Estimation Method" "obs Observations" "r R-squared") sfmt(%10.0f %10.0f %10.0f %10.0f %10.0f %10.3f) ///
align(cccc) label nogaps numbers nomtitles nonotes keep(r2_rain l_inc_ihs r2_l_inc_ihs lag_Exp) 

*-------------------------------------------------------------------------------
**** Table 4 : Test overidentifying restrictions on reduced form for consumption
*-------------------------------------------------------------------------------

use monthly_annualized_data531_new.dta, clear

* Cultivation vs Livestock (Table 4 : Column 1 & Table 4 : Column 2) 
sort id year
tsset id year

cap drop sample1 sample2

qui reg lnexp_pc l.cult_inc1 l.live_inc1 l2.cult_inc1 l2.live_inc1 l3.cult_inc1 l3.live_inc1
gen sample1=1 if e(sample)

qui reg l.lnexp_pc l.cult_inc1 l.live_inc1 l2.cult_inc1 l2.live_inc1 l3.cult_inc1 l3.live_inc1
gen sample2=1 if e(sample)

reg lnexp_pc l.cult_inc1 l.live_inc1 l2.cult_inc1 l2.live_inc1 l3.cult_inc1 l3.live_inc1 _Iid_* if sample1==1&sample2==1
est store ct

reg l.lnexp_pc l.cult_inc1 l.live_inc1 l2.cult_inc1 l2.live_inc1 l3.cult_inc1 l3.live_inc1 _Iid_* if sample1==1&sample2==1
est store clag

suest ct clag, cl(id)

testnl [ct_mean]l.cult_inc/[clag_mean]l.cult_inc =[ct_mean]l.live_inc/[clag_mean]l.live_inc

eret2 scalar FF1=r(chi2)
eret2 scalar FP1=r(p)


*Fish vs Cultivation (Table 4 : Column 3, Table 4 : Column 4);
sort id year
tsset id year

cap drop sample1 sample2

qui reg lnexp_pc l.fish_inc1 l.cult_inc1 l2.fish_inc1 l2.cult_inc1 l3.fish_inc1 l3.cult_inc1
gen sample1=1 if e(sample)

qui reg l.lnexp_pc l.fish_inc1 l.cult_inc1 l2.fish_inc1 l2.cult_inc1 l3.fish_inc1 l3.cult_inc1
gen sample2=1 if e(sample)

reg lnexp_pc l.fish_inc1 l.cult_inc1  l2.fish_inc1 l2.cult_inc1 l3.fish_inc1 l3.cult_inc1 _Iid_* if sample1==1&sample2==1
est store ct1

reg l.lnexp_pc l.fish_inc1 l.cult_inc1  l2.fish_inc1 l2.cult_inc1 l3.fish_inc1 l3.cult_inc1 _Iid_* if sample1==1&sample2==1
est store clag1

suest ct1 clag1, cl(id)

testnl [ct1_mean]l.fish_inc/[clag1_mean]l.fish_inc =[ct1_mean]l.cult_inc/[clag1_mean]l.cult_inc

eret2 scalar FF12=r(chi2)
eret2 scalar FP12=r(p)


*** Export Table 3
esttab ct clag ct1 clag1 using "${tex_tables}T4.tex" , ///
replace  noobs b(%10.4f) se wrap nostar ///
scalars("N Observations" "chi Chi-sqared statistic (p-value) on ratios of t-1 coefficients equal") sfmt(%10.0f %10.3f ) ///
align(cccc) label nogaps numbers nomtitles nonotes drop(_cons _Iid_*) order(L.cult_inc1 L2.cult_inc1 L3.cult_inc1 L.live_inc1  L2.live_inc1 L3.live_inc1  L.fish_inc1  L2.fish_inc1 L3.fish_inc1) 

****************************************************************************************************
*TABLES IN APPENDIX B
****************************************************************************************************

*-------------------------------------------------------------------------------
**** Amnesia Table B1 : (Appendix table B1: Testing amnesia)
*-------------------------------------------------------------------------------
use monthly_annualized_data531_new.dta, clear
/* 
Regress current inverse marginal utility on lagged inverse marginal utility and its interactions  
With being in the 2nd, 3rd or 4th quartile
(higher quartile means more constrained)
*/

*(Table B1 : Column 1)
areg  imu imu_old limuX25 limuX50 limuX75, a(vill) cl(id)
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estadd local fixed "Village"
estadd local sample "Full"
est store all

*(Table B1 : Column 2)
areg  imu lninc imu_old  limuX25 lnincX25 limuX50 lnincX50 limuX75 lnincX75, a(vill) cl(id)
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estadd local fixed "Village"
estadd local sample "Full"

*Can we reject sufficiency for the most-constrained group?
test imu_old+limuX75=0
eret2 scalar FB= _b[imu_old]+_b[limuX75]
eret2 scalar FF=r(F)
eret2 scalar FP=r(p)
est store allinc

*Drop Extreme 10%
*(Table B1 : Column 3)
areg  imu imu_old limuX25 limuX50 limuX75 if ~(exp_pc>highexp95|exp_pc<lowexp05), a(vill) cl(id)
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estadd local fixed "Village"
estadd local sample "Middle 90"
est store mid90

*(Table B1 : Column 4)
areg  imu lninc imu_old  limuX25 lnincX25 limuX50 lnincX50 limuX75 lnincX75 if ~(exp_pc>highexp95|exp_pc<lowexp05), a(vill) cl(id)
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estadd local fixed "Village"
estadd local sample "Middle 90"

*Can we reject sufficiency for the most-constrained group?
test imu_old+limuX75=0
eret2 scalar FB= _b[imu_old]+_b[limuX75]
eret2 scalar FF=r(F)
eret2 scalar FP=r(p)
est store mid90inc

*Drop Extreme 20%
*(Table B1 : Column 5)
areg  imu imu_old limuX25 limuX50 limuX75 if ~(exp_pc>highexp90|exp_pc<lowexp10), a(vill) cl(id)
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estadd local fixed "Village"
estadd local sample "Middle 80"
est store mid80

*(Table B1 : Column 6)
areg  imu lninc imu_old  limuX25 lnincX25 limuX50 lnincX50 limuX75 lnincX75 if ~(exp_pc>highexp90|exp_pc<lowexp10), a(vill) cl(id)
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estadd local fixed "Village"
estadd local sample "Middle 80"
est store mid80inc


*** Export Table B1
esttab all allinc mid90 mid90inc mid80 mid80inc using "${tex_tables}B1.tex" , ///
replace  noobs b(%10.4f) se wrap star(* 0.10 ** 0.05 *** 0.01) ///
scalars("obs Observations" "r R-squared" "fixed Fixed Effects" "sample Sample") sfmt(%10.0f %10.3f) ///
align(cccc) mtitles ("Full Sample" "Full Sample" "Middle 90" "Middle 90" "Middle 80" "Middle 80")label nogaps numbers  nonotes 

****************************************************************************************************
*TABLES IN APPENDIX C
****************************************************************************************************
eststo clear 

*-------------------------------------------------------------------------------
****Table C1: Summary statistics
*-------------------------------------------------------------------------------

** Import Data set of 670 Households intervewed
use  monthly_annualized_data670.dta, clear

* Generate indicator for households appearing in all 84 months of the survey period
egen tag=tag(id)
gen nonfullsample=1-fullsample

*** Export Table C1 Panel A
xi:reg income  i.year i.vill i.occupation  man_total woman_total ygman_total ygwoman_total adolescent_total child_total toddler_total baby_total
predict inc_r, r

xi:reg exp_noimp i.year i.vill i.occupation  man_total woman_total ygman_total ygwoman_total adolescent_total child_total toddler_total baby_total
predict cons_r, r

foreach var in  income  inc_r exp cons_r {
gen `var'_month=`var'/12
bysort id: egen `var'_monthA= mean(`var'_month)
}

local j_rows = 8
local k_columns = 4
mat m = J(`j_rows', `k_columns',.)
local y=1
foreach var in income_monthA exp_monthA inc_r_monthA  cons_r_monthA   full_total adj_total_t man_total woman_total{
sum `var' if  nonfullsample==0
mat m[`y',1] =r(mean)
reg `var' nonfullsample
mat m[`y',2] =_b[nonfullsample]
mat m[`y',3]= (2 * ttail(e(df_r), abs(_b[nonfullsample]/_se[nonfullsample])))
count if tag==1 & `var'!=.
mat m[`y',4] = r(N)
local y = `y'+1
}

mat colnames m =  "531-HH panel mean"  "Non-con obs HH diff" "Pval" "N"
matrix rownames m = "Monthly income" "Monthly expenditure" "Monthly income, resids" "Monthly expenditure, resids" "HH size" "Adult equivalents" "Adult men" "Adult women"

frmttable using "${tex_tables}C1a.tex", statmat(m) sdec(3,3,3,0) ///
varlabels tex fra replace

*** Export Table C1 Panel B 
local j_rows = 6
local k_columns = 4
mat m = J(`j_rows', `k_columns',.)
local y=1

foreach var in  gm1c_vil gm1c_fgn gm2c_vil gm2c_fgn gm3a3 gm3b3 {
gen `var'_month=`var'/12
bysort id: egen `var'_monthA= mean(`var'_month)
sum `var'_monthA if  nonfullsample==0
mat m[`y',1] =r(mean)
reg `var'_monthA nonfullsample
mat m[`y',2] =_b[nonfullsample]
mat m[`y',3]= (2 * ttail(e(df_r), abs(_b[nonfullsample]/_se[nonfullsample])))
count if tag==1 & `var'!=.
mat m[`y',4] = r(N)
local y = `y'+1
}

mat colnames m =  "531-HH panel mean"  "Non-con obs HH diff" "pval" "N"
matrix rownames m = "orgs in village" "orgs in not in village" "for events in village" "for events not in village" "HHs in village" "HHs not in village"

frmttable using "${tex_tables}C1b.tex", statmat(m) sdec(3,3,3,0) ///
varlabels tex fra replace

*** Export Table C1 Panel C
local j_rows = 6
local k_columns = 4
mat m = J(`j_rows', `k_columns',.)
local y=1

foreach var in gm4c_vil gm4c_fgn gm5c_vil gm5c_fgn gm6a3 gm6b3 {
gen `var'_month=`var'/12
bysort id: egen `var'_monthA= mean(`var'_month)
sum `var'_monthA if  nonfullsample==0
mat m[`y',1] =r(mean)
reg `var'_monthA nonfullsample
mat m[`y',2] =_b[nonfullsample]
mat m[`y',3]= (2 * ttail(e(df_r), abs(_b[nonfullsample]/_se[nonfullsample])))
count if tag==1 & `var'!=.
mat m[`y',4] = r(N)
local y = `y'+1
}

mat colnames m =  "531-HH panel mean"  "Non-con obs HH diff" "pval" "N"
matrix rownames m = "orgs in village" "orgs not in village" "events in village" "events not in village" "HHs in village" "HHs not in village"

frmttable using "${tex_tables}C1c.tex", statmat(m) sdec(3,3,3,0) ///
varlabels tex fra replace


*** Export Table C1 Panel D
local j_rows = 10
local k_columns = 4
mat m = J(`j_rows', `k_columns',.)
local y=1

foreach j in 7 23 11 15 61 19 13 31 65 9 {
gen occupation`j'=occupation2==`j'
sum occupation`j' if  nonfullsample==0 
mat m[`y',1] =r(mean)
reg occupation`j' nonfullsample 
mat m[`y',2] =_b[nonfullsample]
mat m[`y',3]= (2 * ttail(e(df_r), abs(_b[nonfullsample]/_se[nonfullsample])))
count if tag==1 & occupation2!=. 
mat m[`y',4] = r(N)
local y = `y'+1
}
mat colnames m =  "531-HH panel mean"  "Non-con obs HH diff" "Pval" "N"
matrix rownames m = "Rice Farmer" "Construction" "Orchard Farmer" "Shrimp/Fish Farmer" "Agricultural wage labor" "Livestock" "Other Crop Farmer" "Non Agricultural Wage Labour" "Other" "Corn Farmer"

frmttable using "${tex_tables}C1d.tex", statmat(m) sdec(3,3,3,0) ///
varlabels tex fra replace


*-------------------------------------------------------------------------------
****Table C2: Testing the hidden income model, full sample
*-------------------------------------------------------------------------------
*Import Data 
use lagged_aggregate_stay ,clear 
*we first define the proper clustering:
global clust vill_qr

* (1) models with rho=1
*Linear model (no splines):
*(Table C2 : Column 1)
reg exp lag_Exp exp_eta eta l_inc_ihs , vce(cluster $clust)
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model1

local qspline_exp "lag_expq1 lag_expq2 lag_expq3"
local qspline_eta "etaq1 etaq2 etaq3"
local qspline_exp_eta "exp_etaq1 exp_etaq2 exp_etaq3"


local fspline_exp "lag_expf1 lag_expf2 lag_expf3"
local fspline_eta "etaf1 etaf2 etaf3"
local fspline_exp_eta "exp_etaf1 exp_etaf2 exp_etaf3"

gen it=eta*lag_Exp

gen lag_exp_eta2=lag_Exp*eta

bspline, xvar(eta) power(2) generate(eta2_q)
bspline, xvar(lag_exp_eta2) power(2) generate(lag_exp_eta2_q)

flexcurv, xvar(eta) power(2) generate(eta2_f) refpts(1475.881 5347.684 21954.77)
flexcurv, xvar(lag_exp_eta2) power(2) generate(lag_exp_eta2_f) refpts(545449.9  3.21e+07 3.01e+09)


*(Table C2 : Column 2)
reg exp `fspline_exp' exp_eta eta l_inc_ihs , nocons vce(cluster $clust)
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model2

*(Table C2 : Column 3)
reg exp lag_expq1 lag_expq2 lag_expq3 eta2_q1 eta2_q2 eta2_q3 lag_exp_eta2_q1 lag_exp_eta2_q2 lag_exp_eta2_q3 l_inc_ihs , nocons vce(cluster $clust)
estadd local Village "Yes"
estadd local Villagec "Yes"
estadd local Estimate "OLS"
estadd scalar obs= e(N)
estadd scalar r= e(r2)
estimates store model3

*** Export Table C2
esttab model1 model2 model3 using "${tex_tables}C2.tex" , ///
replace  noobs b(%10.4f) se wrap nostar ///
scalars("Village Village-month fixed effect?" "Villagec Village-month fixed effect x ct−1?" "Estimate Estimation Method" "obs Observations" "r R-squared") sfmt(%10.0f %10.0f %10.0f %10.0f %10.3f) ///
align(cccc) label nogaps numbers nomtitles nonotes keep(l_inc_ihs lag_Exp)

/***Note: Province IDs are not included for confidentiality reasons so the following regression cannot be run with the posted data.
*-------------------------------------------------------------------------------
****Table C3: Predicting income with rainfall
*-------------------------------------------------------------------------------

use monthly_annualized_data531_new.dta, clear


*Row 1 : Rice Farmer
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==7

*Row 2 : Construction
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==23

*Row 3 : Orchard Farmer
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==11

*Row 4 : Shrimp/Fish Farmer
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==15

*Row 5 : Agricultural wage labor
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==61

*Row 6 : Livestock
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==19

*Row 7 : Other Crop Farmer
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==13

*Row 8 : Non Agricultural Wage Labour
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==31

*Row 9 : Other
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==65

*Row 10 : Corn Farmer
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==9

*** Export Table C3
local j_rows = 10
local k_columns = 2
mat m = J(`j_rows', `k_columns',.)
local y=1


foreach j in 7 23 11 15 61 19 13 31 65 9 {
xi: reg income rain_dev? rain_dev?_2 i.province if occupation2==`j'
mat m[`y',1] =e(r2)
mat m[`y',2] = e(N)
local y = `y'+1
}
mat colnames m =  "Rsq"  "N" 
matrix rownames m = "Rice Farmer" "Construction" "Orchard Farmer" "Shrimp/Fish Farmer" "Agricultural wage labor" "Livestock" "Other Crop Farmer" "Non Agricultural Wage Labour" "Other" "Corn Farmer"

frmttable using "${tex_tables}C3.tex", statmat(m) sdec(3,0) ///
varlabels tex fra replace
*/

*-------------------------------------------------------------------------------
*****Table C4: Testing the hidden income model (CRRA utility, linear estimates)
*-------------------------------------------------------------------------------
use monthly_annualized_data531_new.dta, clear

areg lnExp lnmu_old , a(vill_year) 
predict exp_hat, r

*(Table C4 : Column 1)
reg exp_hat l_lninc, vce(boot)
estadd scalar chi=e(chi2)
estadd scalar pval = e(p)
estadd scalar  obs= e(N)
est store ols

local rainfall_q1 "l_rain_dev1X7 l_rain_dev1_2X7 l_rain_dev1X9 l_rain_dev1_2X9 l_rain_dev1X11 l_rain_dev1_2X11 l_rain_dev1X13 l_rain_dev1_2X13 l_rain_dev1X15 l_rain_dev1_2X15 l_rain_dev1X19 l_rain_dev1_2X19 l_rain_dev1X23 l_rain_dev1_2X23 l_rain_dev1X31 l_rain_dev1_2X31 l_rain_dev1X61 l_rain_dev1_2X61 l_rain_dev1X65 l_rain_dev1_2X65"
local rainfall_q2 "l_rain_dev2X7 l_rain_dev2_2X7 l_rain_dev2X9 l_rain_dev2_2X9 l_rain_dev2X11 l_rain_dev2_2X11 l_rain_dev2X13 l_rain_dev2_2X13 l_rain_dev2X15 l_rain_dev2_2X15 l_rain_dev2X19 l_rain_dev2_2X19 l_rain_dev2X23 l_rain_dev2_2X23 l_rain_dev2X31 l_rain_dev2_2X31 l_rain_dev2X61 l_rain_dev2_2X61 l_rain_dev2X65 l_rain_dev2_2X65"
local rainfall_q3 "l_rain_dev3X7 l_rain_dev3_2X7 l_rain_dev3X9 l_rain_dev3_2X9 l_rain_dev3X11 l_rain_dev3_2X11 l_rain_dev3X13 l_rain_dev3_2X13 l_rain_dev3X15 l_rain_dev3_2X15 l_rain_dev3X19 l_rain_dev3_2X19 l_rain_dev3X23 l_rain_dev3_2X23 l_rain_dev3X31 l_rain_dev3_2X31 l_rain_dev3X61 l_rain_dev3_2X61 l_rain_dev3X65 l_rain_dev3_2X65"
local rainfall_q4 "l_rain_dev4X7 l_rain_dev4_2X7 l_rain_dev4X9 l_rain_dev4_2X9 l_rain_dev4X11 l_rain_dev4_2X11 l_rain_dev4X13 l_rain_dev4_2X13 l_rain_dev4X15 l_rain_dev4_2X15 l_rain_dev4X19 l_rain_dev4_2X19 l_rain_dev4X23 l_rain_dev4_2X23 l_rain_dev4X31 l_rain_dev4_2X31 l_rain_dev4X61 l_rain_dev4_2X61 l_rain_dev4X65 l_rain_dev4_2X65"


*(Table C4 : Column 2)
ivregress 2sls exp_hat (l_lninc =`rainfall_q1' `rainfall_q2' `rainfall_q3' `rainfall_q4') ,vce(boot) 
estadd scalar chi=e(chi2)
estadd scalar pval = e(p)
estadd scalar  obs= e(N)
est store iv


*** Export Table C4
esttab ols iv using "${tex_tables}C4.tex" , ///
replace  noobs b(%10.4f) se wrap nostar ///
scalars("chi Chi-squared" "pval Pvalue" "obs Observations" ) sfmt(%10.3f %10.3f %10.0f) ///
align(cc) label nogaps numbers nomtitles nonotes 

